#!/bin/env python
# -*- coding: gbk -*-

import re
import os
import urllib2
from urlparse import urlparse, urljoin
from urllib import quote
from bs4 import BeautifulSoup

url='http://www.tom61.com/youshenggushilianbo/huibengushilei/2013-05-13/3597.html'


def urlopen(url, timeout, headers = None, refer=None):
	req = urllib2.Request(url)
	if refer: req.add_header('Referer', refer)
	req.add_header('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/37.0.2062.94 Chrome/37.0.2062.94 Safari/537.36')
	if headers:
		for k, v in headers.iteritems():
			req.add_header(k, v)

	# todo: support percentage with content-length, and save block
	r = urllib2.urlopen(req, timeout=timeout)
	html = r.read()
	return html


def parseplaylist(url):
	html = urlopen(url, 10)
	#print html
	soup = BeautifulSoup(html)
	#print soup.find_all('a')
	playlist = []
	for link in soup.find_all('a', attrs={'target': 'play'}):
		#print link
		#print link.get('href')
		playlist.append(link.get('href'))

	return playlist

def getmp3urls(url):
	regex = '{mp3:"(.*)"}'
	playlist = parseplaylist(url)
	mp3_urls = []
	for purl in playlist:
		purl = urljoin(url, purl)
		html = urlopen(purl, 10)
		#print html
		soup = BeautifulSoup(html)
		for script in soup.find_all('script'):
			# print script.text
			m = re.search(regex, script.text)
			if m: mp3_urls.append((m.group(1), purl))

	return mp3_urls


mp3_urls = getmp3urls(url)
#print mp3_urls

headers = {
	'Range': 'bytes=0-',
	'Connection': 'keep-alive',
	'Accept':'*/*',
	'Accept-Encoding':'identity;q=1, *;q=0',
	'Accept-Language':'zh-CN,zh;q=0.8,en;q=0.6',
}

for url, refer in mp3_urls:
	print "get %s" % url
	o = urlparse(url.encode('utf8'))
	name = os.path.basename(o.path)
	eurl = quote(url.encode('utf8'), safe=':/?')
	print "get %s ..." % eurl

	mp3 = urlopen(eurl, 10, headers, refer)
	f = open(name, 'wb')
	f.write(mp3)
	f.close()
	



